package edu.java.ishop.acessor;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public abstract class SocketDatasourceAccessor {

	public SocketDatasourceAccessor() {

	}

	public Object sendRequest(String command) throws IOException, UnknownHostException, ClassNotFoundException {
		try (Socket socket = new Socket(getHost(), getPort());
				ObjectInputStream ois = new ObjectInputStream(
						socket.getInputStream());
				PrintWriter writer = new PrintWriter(socket.getOutputStream());) {
			writer.println(command);
			writer.flush();
			Object res = ois.readObject();
			return res;
		} 
	}

	public abstract int getPort();

	public abstract String getHost();
}